//
//  TTUINavigationView.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/17.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import UIKit

class TTUINavigationView: UIView {

    var titleLabel:UILabel?
    var leftButton:UIButton?
    var rightButton:UIButton?
    var centerLeftButton:UIButton?
    var centerRightButton:UIButton?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.white
        self.isUserInteractionEnabled = true
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setSelfFrame(_ frame:CGRect) {
        self.frame = frame
    }
    
    func initTitleLabel(){
        titleLabel = UILabel.init(frame: CGRect.init(x: SCREEN_WIDTH/2 - 100, y: UIApplication.shared.statusBarFrame.size.height, width: 200, height: 44))
        titleLabel!.textAlignment = .center
        titleLabel!.adjustsFontSizeToFitWidth = true;
        titleLabel!.textColor = BlackBackColor
        titleLabel!.font = UIFont.boldSystemFont(ofSize: 18)
        titleLabel!.autoresizing(index: 1)
        self.addSubview(titleLabel!)
    }
    func setTitle(_ title:String) {
        if titleLabel == nil {
            initTitleLabel()
        }
        UIView.animate(withDuration: 0.25) {
            self.titleLabel!.text = title
        }
    }
    
    func setTitleColor(_ color:UIColor) {
        if titleLabel == nil {
            initTitleLabel()
        }
        UIView.animate(withDuration: 0.25) {
            self.titleLabel!.textColor = color
        }
    }
    
    func initLeftButton() {
        //        leftButton = UIButton.init(type: .system)
        leftButton = UIButton(type: .custom)
        leftButton?.frame = CGRect.init(x: 10, y: UIApplication.shared.statusBarFrame.size.height, width: 44, height: 44)
        
        leftButton?.titleLabel?.textAlignment = .left
        leftButton?.contentHorizontalAlignment = .left
        leftButton?.imageView?.contentMode = .left
        leftButton?.accessibilityLabel = "imageBackButton"
        leftButton?.setTitleColor(RGBHEX(0x303030 ), for: .normal)
        leftButton?.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        self.addSubview(leftButton!)
    }
    
    func initRightButton() {
        rightButton = UIButton.init(type: .custom)
        rightButton?.frame = CGRect.init(x: SCREEN_WIDTH - 54, y: UIApplication.shared.statusBarFrame.size.height, width: 44, height: 44)
        rightButton?.titleLabel?.textAlignment = .right
        rightButton?.contentHorizontalAlignment = .right
        rightButton?.imageView?.contentMode = .right
        rightButton?.setTitleColor(RGBHEX(0x303030 ), for: .normal)
        rightButton?.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        self.addSubview(rightButton!)
    }
    
    func initCenterLeftButton() {
        centerLeftButton = UIButton.init(type: .custom)
        centerLeftButton?.frame = CGRect.init(x: 54, y: UIApplication.shared.statusBarFrame.size.height, width: 69, height: 44)
        centerLeftButton?.titleLabel?.textAlignment = .left
        centerLeftButton?.contentHorizontalAlignment = .left
        centerLeftButton?.imageView?.contentMode = .left
        centerLeftButton?.setTitleColor(RGBHEX(0x303030 ), for: .normal)
        centerLeftButton?.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        self.addSubview(centerLeftButton!)
    }
    
    func initCenterRightButton() {
        centerRightButton = UIButton.init(type: .custom)
        centerRightButton?.frame = CGRect.init(x: SCREEN_WIDTH - 123, y: UIApplication.shared.statusBarFrame.size.height, width: 69, height: 44)
        centerRightButton?.titleLabel?.textAlignment = .right
        centerRightButton?.contentHorizontalAlignment = .right
        centerRightButton?.imageView?.contentMode = .right
        centerRightButton?.setTitleColor(RGBHEX(0x303030 ), for: .normal)
        centerRightButton?.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        self.addSubview(centerRightButton!)
    }
    
    func setLeftButtonTitle(title:String,target: Any?,sel:Selector)  {
        if leftButton == nil {
            initLeftButton()
        }
        leftButton?.setImage(nil, for: .normal)
        leftButton?.setImage(nil, for: .selected)
        leftButton?.setTitle(title, for: .normal)
        leftButton?.addTarget(target, action: sel, for: .touchUpInside)
    }
    
    func setLeftButtonTitleColor(_ color:UIColor) {
        if leftButton == nil {
            initLeftButton()
        }
        UIView.animate(withDuration: 0.25) {
            self.leftButton?.setTitleColor(color, for: .normal)
        }
    }
    
    func setLeftButtonImage(imgName:String,target: Any?,sel:Selector)  {
        if leftButton == nil {
            initLeftButton()
        }
        leftButton?.setTitle(nil, for: .normal)
        leftButton?.setTitle(nil, for: .selected)
        leftButton?.setImage(UIImage.init(named: imgName), for: .normal)
        leftButton?.addTarget(target, action: sel, for: .touchUpInside)
    }
    
    func setRightButtonTitle(title:String,target: Any?,sel:Selector?)  {
        if rightButton == nil {
            initRightButton()
        }
        rightButton?.setImage(nil, for: .normal)
        rightButton?.setImage(nil, for: .selected)
        rightButton?.setTitle(title, for: .normal)
        if sel != nil {
            rightButton?.addTarget(target, action: sel!, for: .touchUpInside)
        }
    }
    
    func setRightButtonTitleColor(_ color:UIColor) {
        if rightButton == nil {
            initRightButton()
        }
        UIView.animate(withDuration: 0.25) {
            self.rightButton?.setTitleColor(color, for: .normal)
        }
    }
    
    func setRightButtonImage(imgName:String,target: Any?,sel:Selector?)  {
        if rightButton == nil {
            initRightButton()
        }
        rightButton?.setTitle(nil, for: .normal)
        rightButton?.setTitle(nil, for: .selected)
        rightButton?.setImage(UIImage.init(named: imgName), for: .normal)
        if sel != nil {
            rightButton?.addTarget(target, action: sel!, for: .touchUpInside)
        }
    }
    
    func setCenterLeftButtonTitle(title:String,target: Any?,sel:Selector?)  {
        if centerLeftButton == nil {
            initCenterLeftButton()
        }
        centerLeftButton?.setImage(nil, for: .normal)
        centerLeftButton?.setImage(nil, for: .selected)
        centerLeftButton?.setTitle(title, for: .normal)
        if sel != nil {
            centerLeftButton?.addTarget(target, action: sel!, for: .touchUpInside)
        }
    }
    
    func setCenterLeftButtonTitleColor(_ color:UIColor) {
        if centerLeftButton == nil {
            initCenterLeftButton()
        }
        UIView.animate(withDuration: 0.25) {
            self.centerLeftButton?.setTitleColor(color, for: .normal)
        }
    }
    
    func setCenterLeftButtonImage(imgName:String,target: Any?,sel:Selector?)  {
        if centerLeftButton == nil {
            initCenterLeftButton()
        }
        centerLeftButton?.setTitle(nil, for: .normal)
        centerLeftButton?.setTitle(nil, for: .selected)
        centerLeftButton?.setImage(UIImage.init(named: imgName), for: .normal)
        if sel != nil {
            centerLeftButton?.addTarget(target, action: sel!, for: .touchUpInside)
        }
    }
    
    func setCenterRightButtonTitle(title:String,target: Any?,sel:Selector?)  {
        if centerRightButton == nil {
            initCenterRightButton()
        }
        centerRightButton?.setImage(nil, for: .normal)
        centerRightButton?.setImage(nil, for: .selected)
        centerRightButton?.setTitle(title, for: .normal)
        if sel != nil {
            centerRightButton?.addTarget(target, action: sel!, for: .touchUpInside)
        }
    }
    
    func setCenterRightButtonTitleColor(_ color:UIColor) {
        if centerRightButton == nil {
            initCenterRightButton()
        }
        UIView.animate(withDuration: 0.25) {
            self.centerRightButton?.setTitleColor(color, for: .normal)
        }
    }
    
    func setCenterRightButtonImage(imgName:String,target: Any?,sel:Selector?)  {
        if centerRightButton == nil {
            initCenterRightButton()
        }
        centerRightButton?.setTitle(nil, for: .normal)
        centerRightButton?.setTitle(nil, for: .selected)
        centerRightButton?.setImage(UIImage.init(named: imgName), for: .normal)
        if sel != nil {
            centerRightButton?.addTarget(target, action: sel!, for: .touchUpInside)
        }
    }
    
    func setTitleFont(font:UIFont) {
        self.titleLabel?.font = font
    }
    
    func setShadow()  {
        self.setShadow(color: RGBA(r: 0, g: 0, b: 0, a: 1), offset: CGSize.init(width: 0, height: 2), opacity: 0.04, radius: 2)
    }

}
